package com.csd.dao;

// default package

import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.csd.pojo.ProjectType;

/**
 * A data access object (DAO) providing persistence and search support for
 * ProjectType entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see .ProjectType
 * @author MyEclipse Persistence Tools
 */

public class ProjectTypeDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(ProjectTypeDAO.class);
	// property constants
	public static final String NAME = "name";
	public static final String DESCRIPTION = "description";
	public static final String ENABLE = "enable";

	protected void initDao() {
		// do nothing
	}

	/**
	 * ������Ŀ���
	 * 
	 * @param transientInstance
	 *            ��Ŀ���ʵ��
	 */
	public void save(ProjectType transientInstance) {
		log.debug("saving ProjectType instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/**
	 * ɾ����Ŀ���
	 * 
	 * @param persistentInstance
	 *            ��Ŀ���ʵ��
	 */
	public void delete(ProjectType persistentInstance) {
		log.debug("deleting ProjectType instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/**
	 * ������Ŀ���
	 * 
	 * @param persistentInstance
	 *            ��Ŀ���ʵ��
	 */
	public void update(ProjectType persistentInstance) {
		log.debug("updating ProjectType instance");
		try {
			getHibernateTemplate().update(persistentInstance);
			log.debug("update successful");
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}
	}

	/**
	 * �����Ŀ���id����
	 * 
	 * @param id
	 *            ��Ŀ���id
	 * @return ��Ŀ���ʵ��
	 */
	public ProjectType findById(Integer id) {
		log.debug("getting ProjectType instance with id: " + id);
		try {
			System.out.println("==========================" + id);
			ProjectType instance = (ProjectType) getHibernateTemplate().get(
					ProjectType.class, id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/**
	 * ���ʵ��������
	 * 
	 * @param instance
	 *            ��Ŀ���ʵ��
	 * @return ��Ŀ���ʵ���б�
	 */
	public List findByExample(ProjectType instance) {
		log.debug("finding ProjectType instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/**
	 * ���ҿ��ŵ���Ŀ���
	 * 
	 * @return ��Ŀ���ʵ���б�
	 */

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding ProjectType instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from ProjectType as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List findByDescription(Object description) {
		return findByProperty(DESCRIPTION, description);
	}

	public List findByEnable(Object enable) {
		return findByProperty(ENABLE, enable);
	}

	public List findAll() {
		log.debug("finding all ProjectType instances");
		try {
			String queryString = "from ProjectType";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public ProjectType merge(ProjectType detachedInstance) {
		log.debug("merging ProjectType instance");
		try {
			ProjectType result = (ProjectType) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(ProjectType instance) {
		log.debug("attaching dirty ProjectType instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(ProjectType instance) {
		log.debug("attaching clean ProjectType instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}


	@SuppressWarnings("unchecked")
	public int getProjectTypeMaxMemberByID(Integer ID) 
	{
		int max = 0;
		try
		{
			List<ProjectType> list = null;
			
			String queryString = "from ProjectType p where p.id = ?";
			list = (List<ProjectType>)this.getHibernateTemplate().find(queryString, ID);
			
			for(Iterator iterator = list.iterator(); iterator.hasNext();)
			{
				max = Integer.parseInt(((ProjectType)iterator.next()).getMaxiMemberNumber());
			}
			 
		}
		catch (RuntimeException re)
		{
			log.debug("get failed:", re);
			re.printStackTrace();
			throw re;
		}
		
		
		return max;

	}
	
	@SuppressWarnings("unchecked")
	public int getProjectTypeTeacherMemberByID(Integer ID)
	{
		try
		{
			List<ProjectType> list = null;
			
			String queryString = "from ProjectType p where p.id = ?";
			list = (List<ProjectType>)this.getHibernateTemplate().find(queryString, ID);
			
			for(Iterator iterator = list.iterator(); iterator.hasNext();)
			{
				return Integer.parseInt(((ProjectType)iterator.next()).getMaxiMemberNumber());
			}
		}
		catch (RuntimeException re)
		{
			log.debug("get max teacher failed", re);
			re.printStackTrace();
			throw re;
		}
		
		return 0;
	}

	public static ProjectTypeDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (ProjectTypeDAO) ctx.getBean("ProjectTypeDAO");
	}
}